import 'dart:convert';

import 'package:flutter/cupertino.dart';
import 'package:logger/logger.dart';

class QuestionsModel {
  String body;
  List<Options> options;

//String  必有字段  备注：问题类型（单项:SC,多项：MC:问答：C）
  String questionType;
  String questionOrder;
  String isAnswer;
  String notification;
  TextEditingController questionText = TextEditingController();

  QuestionsModel(
      {this.body,
      this.options,
      this.questionType,
      this.notification,
      this.questionOrder,
      this.isAnswer});

  String getStringType() {
    if (questionType == "SC") {
      return "单选";
    } else if (questionType == "MC") {
      return "多选";
    } else if (questionType == "C") {
      return "问答";
    }
    return "";
  }

  QuestionsModel.fromJson(Map<String, dynamic> json) {
    body = json['body'];
    if (json['options'] != null) {
      if (json['options'] is String) {
        List listOptions = jsonDecode(json['options']);
        if (listOptions != null) {
          if (listOptions.length > 0) {
            Logger().d(listOptions);
            options = new List<Options>();
            listOptions.forEach((v) {
              if (v is String) {
                options.add(new Options.fromJson(jsonDecode(v)));
              } else {
                options.add(new Options.fromJson(v));
              }
            });
          }
        }
      } else {
        options = new List<Options>();
        json['options'].forEach((v) {
          options.add(new Options.fromJson(v));
        });
      }
    }

    questionType = json['question_type'];
    questionOrder = json['question_order'];
    isAnswer = json['is_answer'];
    notification = "${json['notification']}";
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['body'] = this.body;
    if (this.options != null) {
      data['options'] = this.options.map((v) => v.toJson()).toList();
    }
    data['question_type'] = this.questionType;
    data['question_order'] = this.questionOrder;
    data['is_answer'] = this.isAnswer;
    return data;
  }
}

class Options {
  //选项
  String order;

  //类型：String  必有字段  备注：选项内容
  String option;

  //类型：String  必有字段  备注：选项类型（其他：Other, 正常：Normal）
  String type;
  //判断是否选中
  bool select;
  //问答题提交
  String answerContent;

  TextEditingController itemText = TextEditingController();

  Options({this.order, this.option, this.type});

  Options.fromJson(Map<String, dynamic> json) {
    select = false;
    order = json['order'];
    option = json['option'];
    type = json['type'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['order'] = this.order;
    data['option'] = this.option;
    data['type'] = this.type;
    data['answer_content'] = this.answerContent;
    return data;
  }
}
